package com.delos.rest.controller;

import com.delos.api.bo.UserBO;
import com.delos.api.converter.UserConverter;
import com.delos.api.service.IUserService;
import com.delos.rpc.dto.UserDTO;
import com.delos.rpc.vo.UserVO;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 测试代码
 *
 * @author zouyaowen
 * @date 2020-05-17
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("根据系统ID获取用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "系统ID", dataTypeClass = Long.class)
    })
    @GetMapping("/get-user")
    public UserVO getUser(Long userId) {
        UserBO user = userService.getUser(userId);
        if (user == null) {
            return null;
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

    @ApiOperation("使用缓存根据系统ID获取用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "系统ID", dataTypeClass = Long.class)
    })
    @GetMapping("/get-user-cache")
    public UserVO getUserCache(Long userId) {
        UserBO user = userService.getUserCache(userId);
        if (user == null) {
            return null;
        }
        // UserVO userVO = new UserVO();
        // BeanUtils.copyProperties(user, userVO);
        UserVO userVO = UserConverter.INSTANCE.toVOFromBO(user);
        return userVO;
    }

    @ApiOperation("修改用户更新缓存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDTO", value = "用户信息", dataTypeClass = UserDTO.class)
    })
    @PutMapping("/modify-user-cache")
    public void modifyUserCache(UserDTO userDTO) {
        userService.modifyUserCache(userDTO);
    }

    @ApiOperation("使用缓存根据系统ID删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "系统ID", dataTypeClass = Long.class)
    })
    @DeleteMapping("/delete-user-cache")
    public void removeUserCache(Long userId) {
        userService.removeUserCache(userId);
    }

    @ApiOperation("添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDTO", value = "用户信息", dataTypeClass = UserDTO.class)
    })
    @PostMapping("/add-user")
    public void addUser(UserDTO userDTO) {
        userService.addUser(userDTO);
    }

    @ApiOperation("添加用户JSON格式")
    @PostMapping("/add-user-json")
    public void addUserJson(@RequestBody @ApiParam UserDTO userDTO) {
        userService.addUser(userDTO);
    }
}
